package io.renren.modules.lucky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.app.constants.StringConstants;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.validator.Assert;
import io.renren.modules.lucky.dao.AppGoodsAddrDao;
import io.renren.modules.lucky.dao.AppGoodsCommentDao;
import io.renren.modules.lucky.dao.AppGoodsInfoDao;
import io.renren.modules.lucky.dao.AppUserAddrDao;
import io.renren.modules.lucky.entity.AppGoodsAddrEntity;
import io.renren.modules.lucky.entity.AppGoodsCommentEntity;
import io.renren.modules.lucky.entity.AppGoodsInfoEntity;
import io.renren.modules.lucky.entity.AppUserAddrEntity;
import io.renren.modules.lucky.service.AppGoodsAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

@Service("appGoodsAddrService")
public class AppGoodsAddrServiceImpl extends ServiceImpl<AppGoodsAddrDao, AppGoodsAddrEntity> implements AppGoodsAddrService {

    @Autowired
    private AppGoodsInfoDao appGoodsInfoDao;

    @Autowired
    private AppGoodsCommentDao appGoodsCommentDao;

    @Autowired
    private AppGoodsAddrDao appGoodsAddrDao;

    @Autowired
    private AppUserAddrDao appUserAddrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AppGoodsAddrEntity> page = this.page(
                new Query<AppGoodsAddrEntity>().getPage(params), new QueryWrapper<AppGoodsAddrEntity>()
        );
        return new PageUtils(page);
    }

    public String chooseWinner(String userId, String goodsId) {
        //1、判断是否启动人工指定策略
        if("1".equals(StringConstants.winnerFlag)) {
            //2、判断活动是否存在
            AppGoodsInfoEntity infoEntity = appGoodsInfoDao.selectOne(new QueryWrapper<AppGoodsInfoEntity>()
                    .eq("id", goodsId)
                    .and(wrapper->wrapper.eq("goods_flag", 2).or().eq("goods_flag", 3)));
            Assert.isNull(infoEntity,"活动不存在");
            //3、判断用户是否参与抽奖
            AppGoodsCommentEntity commentEntity = appGoodsCommentDao.selectOne(new QueryWrapper<AppGoodsCommentEntity>()
                    .eq("goods_id", goodsId)
                    .eq("user_id", userId)
                    .eq("comment_type",2)
                    .eq("comment_flag",1).orderByAsc("create_time").last("limit 0, 1"));
            Assert.isNull(commentEntity,"用户不存在");
            //4、调整活动商品价格
            infoEntity.setGoodsShowPrice(commentEntity.getCommentContent());
            appGoodsInfoDao.update(infoEntity, new QueryWrapper<AppGoodsInfoEntity>().eq("id", goodsId));
            //5、判断所选用户是否合法
            AppGoodsCommentEntity tempCommentEntity = appGoodsCommentDao.selectOne(new QueryWrapper<AppGoodsCommentEntity>()
                    .eq("goods_id", goodsId)
                    .eq("comment_type",2)
                    .eq("comment_flag",1)
                    .eq("comment_content",commentEntity.getCommentContent())
                    .orderByAsc("create_time").last("limit 0, 1"));
            if(commentEntity.getUserId()!=tempCommentEntity.getUserId())
                throw new RRException("指定中奖用户不合法（有在您之前猜中价格的用户）");
            else {
                //6、查找用户默认地址
                AppUserAddrEntity addrEntity = appUserAddrDao.selectOne(new QueryWrapper<AppUserAddrEntity>()
                        .eq("user_id", userId)
                        .eq("addr_flag",1)
                        .orderByAsc("create_time").last("limit 0, 1"));
                //7、设置用户中奖
                AppGoodsAddrEntity goodsEntity = new AppGoodsAddrEntity();
                goodsEntity.setCreateTime(new Date());
                goodsEntity.setUserId(commentEntity.getUserId());
                goodsEntity.setAddrId(addrEntity.getId());
                goodsEntity.setGoodsId(infoEntity.getId());
                appGoodsAddrDao.insert(goodsEntity);
                return "电话号码："+addrEntity.getAcceptPhone()+"的用户获得"+infoEntity.getGoodsName();
            }
        } else {
            return "请切换中将策略 把flag切换为1";
        }
    }

}